
package com.ymko.knfimage;

import com.ymko.knfimage.setting.SettingMainActivity;
import com.ymko.knfimage.util.ImageDownloader;
import com.ymko.knfimage.util.Utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class KakaoProfileImageActivity extends Activity {
    private static final String TAG = KakaoProfileImageActivity.class.getSimpleName();

    private ArrayList<String[]> mKakaoPathList = null;
    private ArrayList<String[]> mKakaoPathList_full = null;
    
    private KakaoProfileImageAdapter mImageAdapter = null;

    private ProgressDialog mProgressDialog = null;

    private GridView mGridView = null;

    private QuickSliding mQuickSliding = null;

    private final ImageDownloader imageDownloader = new ImageDownloader();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kakao_main);

        Button btnSetting = (Button)findViewById(R.id.setting_btn);
        btnSetting.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(KakaoProfileImageActivity.this,
                        SettingMainActivity.class);
                startActivity(intent);
            }
        });

        mGridView = (GridView)findViewById(R.id.kakao_gridview);

        mKakaoPathList = new ArrayList<String[]>();
        mKakaoPathList = Utils.getPathList(Constant.IMAGE_DEFAULT_FILE_PATH, Constant.IMAGE_TYPE_DEFAULT);

        mKakaoPathList_full = new ArrayList<String[]>();
        mKakaoPathList_full = Utils.getPathList(Constant.IMAGE_FULL_PROFILE_FILE_PATH, Constant.IMAGE_TYPE_FULL);
        
        for(int i = 0 ; i < mKakaoPathList_full.size() ; i++)
        {
            mKakaoPathList.add(mKakaoPathList_full.get(i));

        }
        Log.d("YMKO", mKakaoPathList_full.size() + "");
        Log.d("YMKO", android.os.Environment.getExternalStorageDirectory().getAbsolutePath());

        mImageAdapter = new KakaoProfileImageAdapter(getApplicationContext(), mGridView,
                mKakaoPathList);
        mImageAdapter.getImageDownloader().setMode(ImageDownloader.Mode.LOCAL);
        mGridView.setAdapter(mImageAdapter);
        
        if(mKakaoPathList == null || mKakaoPathList.size() <= 0)
        {
            mGridView.setVisibility(View.INVISIBLE);
            LinearLayout llNodata = (LinearLayout) findViewById(R.id.kakao_llNodata);
            llNodata.setVisibility(View.VISIBLE);
        }

        /*
         * ImageAsyncTask asyncTask = new ImageAsyncTask(this,
         * Constant.IMAGE_DEFAULT_FILE_PATH); asyncTask.execute(null);
         */

        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                final int pos = arg2;
                if (mQuickSliding != null && mQuickSliding.isShowing() == true)
                    mQuickSliding.dismiss();

                mQuickSliding = new QuickSliding(arg1, R.layout.kakao_item_slidingaction,
                        R.id.kakao_item_slidinglayout);
                mQuickSliding.setAnimStyle(QuickSliding.ANIM_GROW_FROM_RIGHT);

                ImageView photo = (ImageView)mQuickSliding.getTrack().findViewById(
                        R.id.kakao_item_slidinglayout_ivPhoto);
                imageDownloader.download(mKakaoPathList.get(pos)[0], photo);
                
                ImageView friendList = (ImageView)mQuickSliding.getTrack().findViewById(
                        R.id.kakao_item_slidinglayout_ivFriendlist);
                friendList.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), FriendAddressDialog.class);
                        intent.putExtra("file_path", mKakaoPathList.get(pos)[0]);
                        startActivity(intent);
                    }
                });
                

                ImageView save = (ImageView)mQuickSliding.getTrack().findViewById(
                        R.id.kakao_item_slidinglayout_ivSave);
                save.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), CustomDialog.class);
                        intent.putExtra("file_path", mKakaoPathList.get(pos)[0]);
                        startActivity(intent);
                    }
                });

                mQuickSliding.show();
            }
        });
        
//        if(mGridView.getChildCount() >= 25)
//        {
//            mImageAdapter.setPhoto(0, 25);
//            
//        }
//        else
//        {
//            mImageAdapter.setPhoto(0, mGridView.getChildCount());
//        }
    }

    void showProgress(boolean bShow) {

        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
        }

        if (bShow) {
            mProgressDialog.show();
            mProgressDialog.setContentView(R.layout.progress_dialog_layout);
        } else {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

//    class ImageAsyncTask extends AsyncTask<Void, Void, Boolean> {
//        Context mContext = null;
//
//        String mPath = null;
//
//        public ImageAsyncTask(Context context, String path) {
//            mContext = context;
//            mPath = path;
//        }
//
//        @Override
//        protected void onPreExecute() {
//            super.onPreExecute();
//            showProgress(true);
//        }
//
//        @Override
//        protected Boolean doInBackground(Void... params) {
//            boolean ret = false;
//
//            mKakaoPathList.clear();
//            mKakaoPathList = Utils.getPathList(mPath);
//
//            if (mKakaoPathList != null && mKakaoPathList.size() > 0) {
//
//                ret = true;
//            }
//
//            return ret;
//        }
//
//        @Override
//        protected void onPostExecute(Boolean result) {
//            // TODO Auto-generated method stub
//            super.onPostExecute(result);
//
//            showProgress(false);
//            this.onCancelled();
//
//            if (result) {
//                if (mImageAdapter != null) {
//                    mImageAdapter.notifyDataSetChanged();
//                }
//            }
//        }
//
//        @Override
//        protected void onCancelled() {
//            super.onCancelled();
//        }
//    }
}
